﻿ using MediatR;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.MaterialStoreCommand;
using T.Materials.Repository.MaterialsInterface;

namespace T.Materials.Read.API.ApplicationRead.CommandHandler.MaterialStoreCommandHandler
{
    /// <summary>
    /// 显示指定仓储物资信息命令处理器
    /// </summary>
    public class ShowStoreByIdCommandHandler : IRequestHandler<ShowStoreByIdCommand, List<MaterialStoreDto>>
    {
        /// <summary>
        /// 物资与入库仓储接口
        /// </summary>
        protected readonly IMaterialStoreRepository materialStoreRepository;

        /// <summary>
        /// 国家仓储接口
        /// </summary>
        protected readonly ICountryRepository countryRepository;

        /// <summary>
        /// 省份仓储接口
        /// </summary>
        protected readonly IEconomizeRepository economizeRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialStoreRepository">物资与入库仓储接口</param>
        /// <param name="countryRepository">国家仓储接口</param>
        /// <param name="economizeRepository">省份仓储接口</param>
        public ShowStoreByIdCommandHandler(IMaterialStoreRepository materialStoreRepository, ICountryRepository countryRepository, IEconomizeRepository economizeRepository)
        {
            this.materialStoreRepository = materialStoreRepository;
            this.countryRepository = countryRepository;
            this.economizeRepository = economizeRepository;
        }

        /// <summary>
        /// 处理显示指定仓储物资信息命令
        /// </summary>
        /// <param name="request">显示指定仓储物资信息命令</param>
        /// <param name="cancellationToken">取消标记</param>
        /// <returns>返回指定ID的物资入库仓储列表</returns>
        public async Task<List<MaterialStoreDto>> Handle(ShowStoreByIdCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await materialStoreRepository.GetAllAsync()
                        join b in await countryRepository.GetAllAsync()
                        on a.CountryId equals b.CountryId
                        join c in await economizeRepository.GetAllAsync()
                        on a.EconomizeId equals c.EconomizeId
                        where !a.IsDeleted && a.MaterialStoreId == request.MaterialStoreId && b.CountryId==c.CountryId /*&& a.MaterialClassificat==2*/
                        select new MaterialStoreDto
                        {
                            MaterialStoreId = a.MaterialStoreId,
                            NameOfGoods = a.NameOfGoods,
                            MaterialStoreType = a.MaterialStoreType,
                            Material = a.Material,
                            Specifications = a.Specifications,
                            CountryId = a.CountryId,
                            CountryName = b.CountryName,
                            ApprovalStatus=a.ApprovalStatus,
                            Number = a.Number,
                        }).Distinct().ToList();
            return list;
        }
    }
}
